package com.demo.shardingsphere.order.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;

/**
 * 分库分表配置
 */
@Configuration
public class OrderShardingConfig {

   @Bean
    public DataSource shardingCustomer() throws SQLException {

        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();

        //  配置多数据源
        Map<String, DataSource> dataSourceMap = getDatasourceMap();
        //  其它配置信息
        Properties properties = new Properties();
        // 打印出分库路由后的sql
        properties.setProperty("sql.show", "true");

        //  (分库分表)策略配置
        List<TableRuleConfiguration> allTableRuleConfiguration = getAllTableRuleConfiguration();

        shardingRuleConfig.getTableRuleConfigs().addAll(allTableRuleConfiguration);
        //   创建并返回sharding总数据源,注入容器
        return ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, properties);
    }


    public static List<TableRuleConfiguration> getAllTableRuleConfiguration() {
        List<TableRuleConfiguration> list = new ArrayList<>(8);
        //订单号查询配置
        TableRuleConfiguration userOrderRule = getOrderNoTableRuleConfiguration();
        //用户id分表订单表配置
        TableRuleConfiguration userIdRule = getMemberIdTableRuleConfiguration();

        list.add(userOrderRule);
        list.add(userIdRule);
        return list;
    }

    /**
     * 订单号查询分库分表规则配置
     * @return
     */
    private static TableRuleConfiguration getOrderNoTableRuleConfiguration() {
        //  逻辑表名 t_order$
         String nodes = "db0.t_order${0..1}";

        //表规则配置，t_order_no 虚拟表名
        TableRuleConfiguration staffTableRuleConfig = new TableRuleConfiguration("t_order_no", nodes);
        //数据库分片列 order_no
        staffTableRuleConfig.setDatabaseShardingStrategyConfig(
                new StandardShardingStrategyConfiguration("order_no", new OrderNoQueryDatabaseAlgorithm()));
        //表分片列 order_no
        StandardShardingStrategyConfiguration standardShardingStrategyConfiguration = new StandardShardingStrategyConfiguration(
                "order_no", new OrderNoQueryTableAlgorithm());
        staffTableRuleConfig.setTableShardingStrategyConfig(standardShardingStrategyConfiguration);

        return staffTableRuleConfig;
    }

    /**
     *  会员id 分库分表规则配置
     * @return
     */
    private static TableRuleConfiguration getMemberIdTableRuleConfiguration() {
        //  逻辑表名 t_order$
        String nodes = "db0.t_order${0..1}";
        //表规则配置
        TableRuleConfiguration staffTableRuleConfig = new TableRuleConfiguration("t_order", nodes);
        //数据库分片
        staffTableRuleConfig.setDatabaseShardingStrategyConfig(
                new StandardShardingStrategyConfiguration("member_id", new OrderDatabaseAlgorithm()));
        //表分片
        StandardShardingStrategyConfiguration standardShardingStrategyConfiguration = new StandardShardingStrategyConfiguration(
                "member_id", new OrderTableAlgorithm());
        staffTableRuleConfig.setTableShardingStrategyConfig(standardShardingStrategyConfiguration);

        return staffTableRuleConfig;
    }

    public static Map<String, DataSource> getDatasourceMap() {

        Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(4);

        DruidDataSource db0 = new DruidDataSource();
        db0.setDriverClassName("com.mysql.cj.jdbc.Driver");
        db0.setUrl("jdbc:mysql://106.12.59.2:3306/db0?characterEncoding=utf8&serverTimezone=GMT%2B8");
        db0.setUsername("root");
        db0.setPassword("1225@root@1225");
        dataSourceMap.put("db0", db0);


        DruidDataSource db1 = new DruidDataSource();
        db1.setDriverClassName("com.mysql.cj.jdbc.Driver");
        db1.setUrl("jdbc:mysql://106.12.59.2:3306/db1?characterEncoding=utf8&serverTimezone=GMT%2B8");
        db1.setUsername("root");
        db1.setPassword("1225@root@1225");
        dataSourceMap.put("db1", db1);

      /*   DruidDataSource db2 = new DruidDataSource();
        db2.setDriverClassName("com.mysql.cj.jdbc.Driver");
        db2.setUrl("jdbc:mysql://106.12.59.2:3307/db0?characterEncoding=utf8&serverTimezone=GMT%2B8");
        db2.setUsername("root");
        db2.setPassword("1225@root@1225");
        dataSourceMap.put("db2", db2);


        DruidDataSource db3 = new DruidDataSource();
        db3.setDriverClassName("com.mysql.cj.jdbc.Driver");
        db3.setUrl("jdbc:mysql://106.12.59.2:3307/db1?characterEncoding=utf8&serverTimezone=GMT%2B8");
        db3.setUsername("root");
        db3.setPassword("1225@root@1225");
        dataSourceMap.put("db3", db3);*/

        return dataSourceMap;
    }

}